package com.yoda.mall.mysql.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yoda.common.exception.BusinessException;
import com.yoda.common.exception.ErrorResult;
import com.yoda.mall.api.MenuApi;
import com.yoda.mall.api.RoleMenuRelationApi;
import com.yoda.mall.model.entity.ums.Menu;
import com.yoda.mall.model.vo.MenuTreeVo;
import com.yoda.mall.model.vo.PageResult;
import com.yoda.mall.mysql.mapper.MenuMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;


import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@DubboService
public class MenuApiImpl implements MenuApi {
    @Resource
    private MenuMapper menuMapper;
    @Resource
    private RoleMenuRelationApi roleMenuRelationApi;

    @Override
    public PageResult<Menu> pageList(Integer parentId, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Menu::getParentId,parentId).orderByAsc(Menu::getId);
        Page<Menu> page = new Page<>(pageNum,pageSize);
        menuMapper.selectPage(page, wrapper);
        return  new  PageResult<Menu>(pageNum,pageSize,Convert.toInt(page.getTotal()),page.getRecords());
    }

    @Override
    public List<MenuTreeVo> treeList(Integer parentId) {
        List<MenuTreeVo> menuTreeList = new ArrayList<>();
        LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Menu::getParentId,parentId).orderByAsc(Menu::getId);
        List<Menu> menuList = menuMapper.selectList(wrapper);
        if(CollUtil.isNotEmpty(menuList)){
            for (Menu menu : menuList) {
                MenuTreeVo menuTree = new MenuTreeVo();
                BeanUtils.copyProperties(menu,menuTree);
                menuTree.setChildren(treeList(Convert.toInt(menu.getId())));
                menuTreeList.add(menuTree);
            }
        }
        return menuTreeList;
    }

    @Override
    public void create(Menu menu) {
        menuMapper.insert(menu);
    }

    /**
     * 删除菜单和其对应的角色
     * @param id
     */
    @Override
    public void delete(Integer id) {
        LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Menu::getParentId,id);
        List<Menu> menuList = menuMapper.selectList(wrapper);
        if(CollUtil.isEmpty(menuList)){
            menuMapper.deleteById(id);
            roleMenuRelationApi.deleteByMenuId(id);
        }else{
            throw new BusinessException(ErrorResult.menuError());
        }
    }

    @Override
    public Menu findById(Integer id) {
        return menuMapper.selectById(id);
    }

    @Override
    public void updateById(Integer id,Menu menu) {
        menu.setId(Convert.toLong(id));
        menuMapper.updateById(menu);
    }

}
